﻿using System.Text.Json.Serialization;
using System.Text.Json;

namespace WeChatBotSDK.JsonEx
{
    public class IntConverter : JsonConverter<int>
    {
        public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.Number && reader.TryGetInt32(out int value))
                return value; // 如果是数字，返回数字值
            if (reader.TokenType == JsonTokenType.String)
            {
                var stringValue = reader.GetString();
                if (int.TryParse(stringValue, out int intValue))
                    return intValue; // 尝试将字符串转换为数字
            }

            return 0; // 无法解析为数字时返回 null
        }

        public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options)
        {
            writer.WriteNumberValue(value);
        }
    }
    public class NullableDoubleConverter : JsonConverter<double?>
    {
        public override double? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.Null)
                return null; // 如果是 null，返回 null
            if (reader.TokenType == JsonTokenType.Number && reader.TryGetDouble(out double value))
                return value; // 如果是数字，返回数字值
            if (reader.TokenType == JsonTokenType.String)
            {
                var stringValue = reader.GetString();
                if (double.TryParse(stringValue, out double intValue))
                    return intValue; // 尝试将字符串转换为数字
            }

            return null; // 无法解析为数字时返回 null
        }

        public override void Write(Utf8JsonWriter writer, double? value, JsonSerializerOptions options)
        {
            if (value.HasValue)
                writer.WriteNumberValue(value.Value);
            else
                writer.WriteNullValue();
        }
    }

    public class StringToEnumConverter<T> : JsonConverter<T> where T : struct, Enum
    {
        public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.Number)
            {
                // 如果是数字，直接转换为枚举
                var value = reader.GetInt32();
                return (T)Enum.ToObject(typeof(T), value);
            }

            if (reader.TokenType == JsonTokenType.String)
            {
                // 如果是字符串形式的数字，尝试解析
                var stringValue = reader.GetString();
                if (int.TryParse(stringValue, out int numericValue))
                {
                    return (T)Enum.ToObject(typeof(T), numericValue);
                }
            }

            throw new JsonException($"Unable to convert {reader.GetString()} to Enum {typeof(T)}.");
        }

        public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
        {
            // 将枚举的数值写出为数字
            writer.WriteNumberValue(Convert.ToInt32(value));
        }
    }

}
